package com.wangp.userpermission.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangp.userpermission.dto.UserPermissionDTO;
import com.wangp.userpermission.dto.UserPermissionQueryReqDTO;
import com.wangp.userpermission.dto.UserPermissionQueryRespDTO;

import com.wangp.userpermission.mapper.PermissionMapper;
import com.wangp.userpermission.mapper.UserMapper;

import com.wangp.userpermission.mapper.UserPermissionMapper;
import com.wangp.userpermission.po.PermissionDO;
import com.wangp.userpermission.po.UserDO;
import com.wangp.userpermission.po.UserPermissionDO;
import com.wangp.userpermission.service.UserPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 第一行简要描述.
 * </p>
 *
 * @author wangping
 * @since 2024-12-24
 */
@Service
public class UserPermissionServiceImpl implements UserPermissionService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private UserPermissionMapper userPermissionMapper;

        @Override
    public UserPermissionQueryRespDTO queryPage(final UserPermissionQueryReqDTO userPermissionQueryReqDTO) {

            int page = userPermissionQueryReqDTO.getPage();
            int size = userPermissionQueryReqDTO.getSize();
            Page<UserDO> selectpage = new Page<>(page,size);
            LambdaQueryWrapper<UserDO> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(UserDO::getIsDeleted,0);
            //得到用户信息的分页结果
            Page<UserDO> resultPage  = userMapper.selectPage(selectpage, queryWrapper);
            List<UserDO> records = resultPage .getRecords();
            Long count = userMapper.selectCount(queryWrapper);
            List<UserPermissionDTO>  respDTOList = converToUserPermissionQueryRespDTO(records);

//            for (final UserPermissionDTO userDTO : respDTOList) {
//                UserDO userDO = userMapper.selectById(userDTO.getId());
//                UserPermissionDO userPermissionDO = userPermissionMapper.selectById(userDO.getId());
//                userDTO.setUserId(userPermissionDO.getUserId());
//            }
//
//            for (final UserPermissionDTO permissionDTO : respDTOList) {
//                UserPermissionDO userPermissionDO = userPermissionMapper.selectById(permissionDTO.getUserId());
//                permissionDTO.setPermissionId(userPermissionDO.getPermissionId());
//            }

            UserPermissionQueryRespDTO queryRespDTO = new UserPermissionQueryRespDTO();
             queryRespDTO.setPage(page);
             queryRespDTO.setSize(size);
             queryRespDTO.setTotal(count);
             queryRespDTO.setData(respDTOList);

             return queryRespDTO;
    }

    @Override
    public UserPermissionDTO queryInfoById(final Integer id) {
        UserPermissionDTO userPermissionDTO = new UserPermissionDTO();
        //用户表
        UserDO userDO = userMapper.selectById(id);
        //公共表
        LambdaQueryWrapper<UserPermissionDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserPermissionDO::getUserId,userDO.getId());
        List<UserPermissionDO> userPermissionDOList = userPermissionMapper.selectList(wrapper);
        //得到公共表 permissionId;
        for (final UserPermissionDO userPermissionDO : userPermissionDOList) {
            //回填permissionId
            userPermissionDTO.setPermissionId(userPermissionDO.getPermissionId());
        }
        LambdaQueryWrapper<PermissionDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PermissionDO::getId,userPermissionDTO.getPermissionId());
        List<PermissionDO> permissionDOList = permissionMapper.selectList(queryWrapper);

        for (PermissionDO permissionDO : permissionDOList) {
            //回填权限
            userPermissionDTO.setPermissionName(permissionDO.getPermissionName());
            userPermissionDTO.setDescription(permissionDO.getDescription());
            //回填用户
            userPermissionDTO.setUserId(userDO.getId());
            userPermissionDTO.setUsername(userDO.getUsername());
            userPermissionDTO.setEmail(userDO.getEmail());
        }
        return userPermissionDTO;
    }

    private List<UserPermissionDTO> converToUserPermissionQueryRespDTO(final List<UserDO> records) {

        List<UserPermissionDTO> userPermissionDTOList = new ArrayList<>();

        for (final UserDO record : records) {
            UserPermissionDTO userPermissionDTO = new UserPermissionDTO();
            userPermissionDTO.setUsername(record.getUsername());
            userPermissionDTO.setId(record.getId());
            userPermissionDTO.setEmail(record.getEmail());
            userPermissionDTOList.add(userPermissionDTO);
        }
        return userPermissionDTOList;

    }

}
